package com.nti56.springboot.service;

import java.util.List;
import java.util.Map;

import com.nti56.springboot.model.Organization;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nti56.springboot.exception.MyRuntimeException;
import com.nti56.springboot.mapper.tmsmapper.ModuleMapper;
import com.nti56.springboot.model.Module;
import com.nti56.springboot.utils.ParamUtil;
import com.nti56.springboot.utils.ParamUtil.Param;
import com.nti56.springboot.utils.StringUtil;

@Service
public class ModuleService {
	
	private static final Logger logger = LoggerFactory.getLogger(ModuleService.class);
	
	@Autowired
    private ModuleMapper moduleMapper;
	
	public List<Module> findAll(Map params) {
		return moduleMapper.findAll(params);
	}
	
	@Transactional(rollbackFor = Exception.class, transactionManager = "tmsTransactionManager") 
	public void addModule(Module m){
		if(!StringUtil.isEmpty(m.getModuleCode())) {
			Param map = ParamUtil.setParam("moduleCode", m.getModuleCode());
			Long cnt = this.moduleMapper.countByMap(map);
			if(cnt>0) {
				throw new MyRuntimeException("资源编码已经存在，不允许添加！");
			}
		}		
		moduleMapper.insert(m);
	}
	
	@Transactional(rollbackFor = Exception.class, transactionManager = "tmsTransactionManager")
	public void updateModule(Module m){
		if(!StringUtil.isEmpty(m.getModuleCode())) {
			Param map = ParamUtil.setParam("moduleCode", m.getModuleCode()).setParam("moduleId", m.getModuleId());
			Long cnt = this.moduleMapper.countByMap(map);
			if(cnt>0) {
				throw new MyRuntimeException("资源编码已经存在，不允许修改！");
			}
		}
		moduleMapper.update(m);
	}
	
	/**
	 * 删除下级资源
	 * 删除当前资源
	 * 删除角色资源中间表
	 * @param moduleId
	 */
	@Transactional(rollbackFor = Exception.class, transactionManager = "tmsTransactionManager")
	public void deleteModule(String moduleId){
		List<Module> sonList = this.moduleMapper.findAll(ParamUtil.setParam("parentId", moduleId));
		if(!CollectionUtils.isEmpty(sonList)) {
			throw new MyRuntimeException("请先删除子菜单!");
		}
		moduleMapper.delete(moduleId);
		moduleMapper.deleteModuleRole(ParamUtil.setParam("moduleId", moduleId));
	}
	
	public Module getById(String moduleId){
		return moduleMapper.getById(moduleId);
	}
	
	public Module getByModuleCode(String moduleCode){
		return moduleMapper.getByModuleCode(moduleCode);
	}
	
	@Transactional(rollbackFor = Exception.class, transactionManager = "tmsTransactionManager") 
	public void saveAuthSetting(String roleId,List<String> moduleIds){
		//先删除中间表，再重新保存
		Map params = ParamUtil.setParam("roleId", roleId);				
		moduleMapper.deleteModuleRole(params);
		for(String moduleId : moduleIds){
			Map map = ParamUtil.setParam("roleId", roleId).setParam("moduleId",moduleId);
			moduleMapper.insertModuleRole(map);
		}
	}
	
	@Transactional(rollbackFor = Exception.class, transactionManager = "tmsTransactionManager") 
	public void saveDataAuthSetting(String roleId,List<String> orgIds){
		//先删除中间表，再重新保存
		Map parm = ParamUtil.setParam("roleId", roleId);				
		moduleMapper.deleteOrgRole(parm);
		for(String orgId : orgIds){
			Map map = ParamUtil.setParam("roleId", roleId).setParam("orgId",orgId);
			moduleMapper.insertOrgRole(map);
		}
	}
	
	/**
	 * 根据角色查询操作资源
	 * @param params
	 * @return
	 */
	public List<Module> getResourceList(Map params){
		return moduleMapper.getResourceList(params);
	}
	
	/**
	 * 根据角色查询数据权限资源
	 * @param params
	 * @return
	 */
	public List<Organization> getAuthDataList(Map params){
		return moduleMapper.getAuthDataList(params);
	}
	
	/**
	 * 根据用户查询系统授权
	 * @param params
	 * @return
	 */
	public List<Module> findAuthResource(Map params){
		return moduleMapper.findAuthResource(params);
	}
	
	/**
	 * 查询用户授权的组织机构编码
	 * @param userId
	 * @return
	 */
	public List<String> getDataAuthOrgCodeList(String userId){
		Map params = ParamUtil.setParam("userId", userId);
		return moduleMapper.getDataAuthOrgCodeList(params);
	}
}
